﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

/// <summary>
/// Descripción breve de Datos
/// </summary>
public class Datos
{
       
        //private string cadena = "Data Source=LUCAS-PC;Initial Catalog=RecetasWeb;Integrated Security=True";
        public SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Main"].ConnectionString);
        private SqlCommandBuilder cmb;
        public DataSet ds = new DataSet();
        public SqlDataAdapter da;
        public SqlCommand comando;

        private void conectar() //conectamos a la base de datos
        {
            SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Main"].ConnectionString);
        }

        public Datos()
        {
            conectar();
        }

        public SqlConnection ObtenerConexion()
        {
            SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Main"].ConnectionString);
            try
            {
                cn.Open();
                return cn;
            }
            catch (Exception)
            {
                return null;
            }
        }

        //Consultar en base de datos
        public void consultar(string sql, string tabla)
        {
            ds.Tables.Clear();
            da = new SqlDataAdapter(sql,cn);
            cmb = new SqlCommandBuilder(da);
            da.Fill(ds, tabla);
        }

        public DataTable consultar_general(string tabla)
        {
            string sql = "select * from " + tabla;
            da = new SqlDataAdapter(sql, cn);
            DataSet dts = new DataSet();
            da.Fill(dts, tabla);
            DataTable dt = new DataTable();
            dt = dts.Tables[tabla];
            return dt;
        }

        public DataTable consultar_especifico(string sql, string tabla)
        {
            comando = new SqlCommand(sql, cn);
            da = new SqlDataAdapter(comando);
            DataTable dt = new DataTable(tabla);
            da.Fill(dt);
            return dt;
        }

        public void AgregarTablaDataSetSQLServer(string ConexionBD, string ConsultaSQL,
                          string NombreTabla, ref DataSet ds)
        {
            SqlConnection cn = new SqlConnection(ConexionBD);
            SqlDataAdapter adaptador = new SqlDataAdapter(ConsultaSQL, cn);
            cn.Open();
            adaptador.Fill(ds, NombreTabla);
        }

        //Eliminar
        public bool eliminar(string tabla, string condicion)
        {
            cn.Open();
            string sql = "delete from " + tabla + " where " + condicion;
            comando = new SqlCommand(sql, cn);
            int i = comando.ExecuteNonQuery();
            cn.Close();
            if (i > 0)
            { 
                return true; 

            } else {

               return false; 
            }
        }

        public bool eliminar(string tabla, string condicion,string condicion2)
        {
            cn.Open();
            string sql = "delete from " + tabla + " where " + condicion + " and " + condicion2;
            comando = new SqlCommand(sql, cn);
            int i = comando.ExecuteNonQuery();
            cn.Close();
            if (i > 0)
            {
                return true;

            }
            else
            {

                return false;
            }
        }

        //Actualizar
        public bool actualizar(string tabla, string campos, string condicion)
        {
            cn.Open();
            string sql = "UPDATE " + tabla + " SET " + campos + " WHERE " + condicion;
            comando = new SqlCommand(sql, cn);
            int i = comando.ExecuteNonQuery();
            cn.Close();
            if (i > 0)
            {
                return true;

            }
            else
            {

                return false;
            }
        }

        //insertar
        public bool insertar(string sql)
        {
            cn.Open();
            comando = new SqlCommand(sql, cn);
            int i = comando.ExecuteNonQuery();
            cn.Close();
            if (i > 0)
            {
                return true;

            }
            else
            {

                return false;
            }  
	    }
}